#include "lcd1602.h"
#include "Motor.h"
volatile int shijian[4]= {0};



typedef struct{
    unsigned char SetTimeMode;
    unsigned char Move;
    unsigned char Add;
    unsigned char Sub;
    unsigned char counter;
    int pos ;
    int time ;
    int count;
}SetTime;

    
volatile SetTime xTime_t_Flag = {0,0,0,0,0,0,0,0};




void Motor_init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    GPIO_InitTypeDef GPIO_init_struct;
    //GPIO_InitTypeDef Dir_GPIO;
    GPIO_init_struct .GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_init_struct .GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_init_struct .GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
    GPIO_Init(GPIOA,&GPIO_init_struct);
    //  Dir_GPIO .GPIO_Mode = GPIO_Mode_Out_PP;
    // Dir_GPIO .GPIO_Speed = GPIO_Speed_50MHz;
    // Dir_GPIO .GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_5;
    // GPIO_Init(GPIOB,&Dir_GPIO);

}


void Motor_control(Motor_Dir Dir)
{
    int i = 0;
    if(Dir == Forward)
        GPIOA ->ODR &= (~(1 << 5));
    if(Dir == Reverse)
        {
            GPIOA ->ODR |= ((1 << 5));
        }
    for(i = 0; i < 2500; i++)
    {
        GPIO_SetBits(GPIOA, GPIO_Pin_4);
        vDelayus(1000);
        GPIO_ResetBits(GPIOA, GPIO_Pin_4);
        vDelayus(300);
    }
}



void vTimeSetTest(void)
{
    if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_2) == Bit_RESET)//加
    {
        vDelayms(5);
        while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_2) == Bit_RESET);
        vDelayms(5);
        if(xTime_t_Flag .SetTimeMode == 1)
            xTime_t_Flag .Add = 1;
        if(xTime_t_Flag .SetTimeMode == 0)
           Motor_control(Reverse); 
        
    }

    if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_13) == Bit_RESET)//减
    {
        vDelayms(5);
        while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_13) == Bit_RESET);
        vDelayms(5);
        if(xTime_t_Flag .SetTimeMode == 1)
            xTime_t_Flag .Sub = 1;
        if(xTime_t_Flag .SetTimeMode == 0)
           Motor_control(Forward); 
    }

    if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6) == Bit_RESET)//光标选择
    {
        vDelayms(5);
        while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6) == Bit_RESET);
        vDelayms(5);
        if(xTime_t_Flag .SetTimeMode == 1)
            xTime_t_Flag .Move = 1;
    }

    if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7) == Bit_RESET)//确认
    {
        vDelayms(5);
        while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7) == Bit_RESET);
        vDelayms(5);
       xTime_t_Flag .SetTimeMode++;
       xTime_t_Flag .SetTimeMode %= 3;
    }
    if(xTime_t_Flag .SetTimeMode == 0)
    {
        xTime_t_Flag .Add = 0;
        xTime_t_Flag .Sub = 0;
        xTime_t_Flag .Move = 0;
    }

}

void vShowTime(void)
{
    
    int i = 0;
    vDisplay_Clear();
    for(i = 0; i < 4; i++)
    {
        if( i == 2)
        {
        vLCD_BusyTest();
		vLcd_Write(':', DATA);
        }
        vLCD_BusyTest();
		vLcd_Write(shijian[i] + 48, DATA);  
    }
}

void SetTimeFun()
{
    int tem = 0;
    if(xTime_t_Flag .SetTimeMode == 1)
    {
        
         if(xTime_t_Flag .Move == 1)
        {
            xTime_t_Flag .pos++;
            xTime_t_Flag .pos %= 4;
            if(xTime_t_Flag .pos > 1)
                vPos(xTime_t_Flag .pos +1, 0);
            else
            vPos(xTime_t_Flag .pos, 0);
                xTime_t_Flag .Move = 0;
        }
        if(xTime_t_Flag .Add == 1)
        {
            shijian[xTime_t_Flag .pos]++;
            shijian[xTime_t_Flag .pos] %= 10;
            vShowTime();
            xTime_t_Flag .Add = 0;
        }
        if(xTime_t_Flag .Sub == 1)
        {
            shijian[xTime_t_Flag .pos]--;
            if(shijian[xTime_t_Flag .pos] < 0)
                shijian[xTime_t_Flag .pos] = 9;
            vShowTime();
            xTime_t_Flag .Sub = 0;
        }
    }
    if(xTime_t_Flag .SetTimeMode == 2)//时间设置完成,进入定时模式
    {
        vDisplay_string(0, 1, "Timer Mode");
        if(xTime_t_Flag .counter == 0)
        {
            xTime_t_Flag .time = (shijian[0]*10 + shijian[1]) *3600 +(shijian[2]*10 + shijian[3])*60;
            TIM_Cmd( TIM3, ENABLE);
            xTime_t_Flag .counter = 1;
        }

        // if( TIM_GetCounter(TIM3) >= 10000)
        // {
        //     xTime_t_Flag .count++;
        //     TIM_SetCounter(TIM3, 0);
        // }

        // if(xTime_t_Flag .count >= xTime_t_Flag .time)
        // {
        //     TIM_SetCounter(TIM3, 0);
        //     TIM_Cmd( TIM3, DISABLE);
        //     xTime_t_Flag .counter = 0;
        //     xTime_t_Flag .SetTimeMode = 0;//退出定时模式
        //     //电机转动
        //     shijian[0] = 0;
        //     shijian[1] = 0;
        //     shijian[2] = 0;
        //     shijian[3] = 0;
        //     vShowTime();
        // }

         if( TIM_GetCounter(TIM3) >= 10000)
        {
            xTime_t_Flag .count++;
            TIM_SetCounter(TIM3, 0);
        }
        if((xTime_t_Flag .count % 60 == 0) && (xTime_t_Flag .count != 0) &&(TIM_GetCounter(TIM3) < 5))
        {
           tem = ((shijian[0]*10 + shijian[1]) *60 +(shijian[2]*10 + shijian[3]) - 1);
           shijian[0] = (tem / 60) / 10;
           shijian[1] = (tem / 60) % 10;
           shijian[2] = ((tem % 60) / 10) % 10;
           shijian[3] = (tem % 60) % 10;
           vShowTime();
        }

        if(xTime_t_Flag .count >= xTime_t_Flag .time)
        {
            TIM_SetCounter(TIM3, 0);
            TIM_Cmd( TIM3, DISABLE);
            xTime_t_Flag .counter = 0;
            xTime_t_Flag .SetTimeMode = 0;//退出定时模式
            //电机转动
            shijian[0] = 0;
            shijian[1] = 0;
            shijian[2] = 0;
            shijian[3] = 0;
            vShowTime();
            Motor_control(Forward);
        }
    }
}


void vTimer_init(u16 psc, u16 arr)
{
    TIM_TimeBaseInitTypeDef Timer_InitStruct;
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    Timer_InitStruct .TIM_ClockDivision = 1;
    Timer_InitStruct .TIM_CounterMode = TIM_CounterMode_Up;
    Timer_InitStruct .TIM_Period = arr;
    Timer_InitStruct .TIM_Prescaler = psc;
    TIM_TimeBaseInit(TIM3, &Timer_InitStruct);
}
